﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Data;
using Extensions;
using System.Collections.Generic;

namespace Aker
{
    public partial class ShelfReportBilecik : XtraReport
    {        
        int EmptyLabelCount;
        public bool Stop = false;
        public ShelfReportBilecik()
        {
            InitializeComponent();
        }

        public ShelfReportBilecik(DataTable shelfs, int emptyLabelCount, int copyCount)
            : this()
        {

            shelfs.TableName = "Table";

            shelfs.Columns.Add("PrintCount", typeof(int));

            DataSet ds = new DataSet();
            ds.Tables.Add(shelfs);

            EmptyLabelCount = emptyLabelCount;

            DataTable dt = ds.Tables[0];
            DataTable dtOrj = dt.Copy(); 
              

            for (int i = 0; i < dtOrj.Rows.Count; i++)
            {
                DataRow drOrj = dtOrj.Rows[i];
                if (drOrj["PrintCount"] == DBNull.Value)
                    drOrj["PrintCount"] = 1;

                int count = (int)drOrj["PrintCount"] - 1;
                for (int j = 0; j < count; j++)
                    dt.ImportRow(drOrj);
            }

            DataView dv = dt.DefaultView;
            //dv.Sort = "Sira";
            DataTable sortedDT = dv.ToTable(); 
            
            ds.Tables.Remove("Table");
            ds.Tables.Add(sortedDT);


            for (int i = 0; i < copyCount - 1; i++)
                ds.Merge(ds.Copy());

            for (int i = 0; i < EmptyLabelCount; i++)
                sortedDT.Rows.InsertAt(sortedDT.NewRow(), 0);
                

            this.DataMember = "Table";
            this.DataSource = ds;

            lblName.DataBindings.Add("Text", null, "Table.Name");
            lblDesc.DataBindings.Add("Text", null, "Table.Description");
            xrBarCode1.DataBindings.Add("Text", null, "Table.ShelfId");
            lblProductId.DataBindings.Add("Text", null, "Table.ShelfId");            
        }       
        
        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            EmptyLabelCount--;
        }
        
        private void ReSize(object sender)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 12f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.WidthF / stringWidth / factor;

            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }    
        
        private void tcKompozisyon_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }        

        private void xrBarCode1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void xrTable2_BeforePrint_1(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void tbKumas_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void xrPictureBox1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void tcArticle_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcDesign_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcWeight_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcWidth_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void xrLabel1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void lblProductId_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }
 
    }
}
